package com.sise.aio和bio和nio.aio.基于aio的读取模式案例;

import org.omg.CORBA.INTERNAL;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousByteChannel;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author idea
 * @data 2019/1/22
 */
public class Test {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        Path path= Paths.get("D:\\project\\io\\test.txt");
        AsynchronousFileChannel channel=AsynchronousFileChannel.open(path);
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        Future<Integer> future=channel.read(byteBuffer,0);
        Integer readNumber=future.get();
        byteBuffer.flip();
        CharBuffer charBuffer=CharBuffer.allocate(1024);
        CharsetDecoder charsetDecoder= Charset.defaultCharset().newDecoder();
        charsetDecoder.decode(byteBuffer,charBuffer,false);
        charBuffer.flip();
        String data=new String(charBuffer.array(),0,charBuffer.limit());
        System.out.println("读取的行数："+readNumber);
        System.out.println("读取的内容："+data);
    }
}
